// http://qunitjs.com/

PepSim.test = {};
PepSim.test.fakeWorld = {
	width: 100,
	height: 100,
	entities: {
		append: function (entity) {
			this.entity = entity;
		}
	},
	get : function (x, y) {
	},
	put : function (x, y, o) {
		this.top = y;
		this.left = x;
	},
	clear : function (x, y) {
		this.cleared = true;
	}
};

PepSim.test.checkEntity = function (entity) {
	ok(entity.type, 'Entity.type must exists');
	ok(typeof entity.type === 'string', 'Entity.type must be a string');
	ok(entity.kill, 'Entity.kill must exists');
	ok(entity.play, 'Entity.kill must exists');
};

module("Core");

test("PepSim.ns", function() {

	PepSim.ns('A.B.C');
	ok(A.B.C, 'A.B.C must exists');
	////////////
	
	PepSim.ns('Test');
	ok(Test, 'Test must exists');
	//////////////

	PepSim.ns('PepSim.Test');
	ok(PepSim.Test, 'PepSim.Test must exists');
	///////////
	A.B.D = 'ciao';
	PepSim.ns('A.B.E');
	ok(A.B.D, 'Field shuold\'t be cleared');

});

test("PepSim.define", function() {
	var obj;
	var Test1 = PepSim.define('Test1', {});
	ok(Test1, 'Test1 must exists');
	ok(new Test1(), 'Test1 must be a constructor');
	////
	PepSim.define('A.B.C', {});
	ok(A.B.C, 'A.B.C must exists');
	////
	PepSim.define('A.B.Pep', {});
	ok(new A.B.Pep(), 'Return a new object');
	////////
	PepSim.define('A.B.Pep2', {
		two: 2,
		init: function (one) {
			this.three = one + this.two;
		},
		add: function (value) {
			this.three = this.three + value;
		}
	});
	obj = new A.B.Pep2(1);
	equal(obj.three, 3);
	obj.add(10);
	equal(obj.three, 13);
	////////
});

module("Food");

test("init & kill", function() {
		
	// TEST: initialization
	var world = PepSim.test.fakeWorld;
	var food = new PepSim.Food(world, 10, 20);
	
	equal(world.entities.entity, food, 'Entity must be added to the list');
	equal(world.top, 20, 'Entity must be in the correct position (top)');
	equal(world.left, 10, 'Entity must be in the correct position (top)');
	
	// TEST: kill
	
	food.kill();
	equal(food.phantom, true, 'Entity must be set as "phantom"');
	equal(world.cleared, true, 'Entiry must be removed from the world');
	
});

test("Check public interface", function() {
	var world = PepSim.test.fakeWorld;
	var food = new PepSim.Food(world, 10, 20);
	PepSim.test.checkEntity(food);
});


module("Pep");

test("init & kill", function() {
		
	// TEST: initialization
	var world = PepSim.test.fakeWorld;
	var pep = new PepSim.Pep(world, 30, 40);
	
	equal(world.entities.entity, pep, 'Entity must be added to the list');
	equal(world.top, 40, 'Entity must be in the correct position (top)');
	equal(world.left, 30, 'Entity must be in the correct position (top)');
	
	// TEST: kill
	
	pep.kill();
	equal(pep.phantom, true, 'Entity must be set as "phantom"');
	equal(world.cleared, true, 'Entiry must be removed from the world');
	
});

test("Check public interface", function() {
	var world = PepSim.test.fakeWorld;
	var pep = new PepSim.Pep(world, 30, 40);
	PepSim.test.checkEntity(pep);
});

module("Feeder");

test("Check public interface", function() {
	var world = PepSim.test.fakeWorld;
	var feeder = new PepSim.Feeder(world, 30, 40);
	PepSim.test.checkEntity(feeder);
});

module("List");

test("Add some element", function() {
	var list = new PepSim.List(),
		count = 0;
	list.append({});
	list.append({});
	list.foreach(function (item) {
		count = count + 1;
	});
	equal(count, 2, 'The enetity must have 2 elements');
});


test("Remove some element", function() {
	var list = new PepSim.List(),
		count = 0,
		first = list.append({}),
		second = list.append({}),
		third = list.append({});
		
	list.removeNode(first);

	list.foreach(function (item) {
		count = count + 1;
	});
	equal(count, 2, 'The entity must have 2 elements');
	
	/////////////////
	list.removeNode(third);
	count = 0;
	list.foreach(function (item) {
		count = count + 1;
	});
	equal(count, 1, 'The entity must have 1 elements');

	/////////////////
	list.removeNode(second);
	count = 0;
	list.foreach(function (item) {
		count = count + 1;
	});
	equal(count, 0, 'The entity must have 0 elements');
});

test("Check phantom flag", function() {
	var list = new PepSim.List(),
		count = 0,
		first = list.append({}),
		second = list.append({}),
		third = list.append({});
		
	third.value.phantom = true;
		
	list.foreach(function (item) {
		count = count + 1;
	});
	equal(count, 2, 'The entity must have 2 elements');
	
	/////////////////
	first.value.phantom = true;;
	count = 0;
	list.foreach(function (item) {
		count = count + 1;
	});
	equal(count, 1, 'The entity must have 1 elements');

	/////////////////
	second.value.phantom = true;
	count = 0;
	list.foreach(function (item) {
		count = count + 1;
	});
	equal(count, 0, 'The entity must have 0 elements');
});

module("World");

test("Add item", function() {
	var world = new PepSim.World(10, 10);
	world.put(2, 3, {});
	ok(world.get(2, 3), 'An item must be at (2;3)');
	ok(!world.get(3, 3), 'No item must be at (3;3)');
	
	world.put(5.12, 6.888, {});
	ok(world.get(5, 6), 'An item must be at (5;6)');
	
});

test("Remove item", function() {
	var world = new PepSim.World(10, 10);
	world.put(2, 3, {});
	world.clear(2, 3);
	ok(!world.get(2, 3), 'No item must be at (2;3)');
	
	world.put(5, 6, {});
	world.clear(5.4234, 6.888);
	ok(!world.get(5, 6), 'No item must be at (5;6)');
	
});